package com.zeny.edu.stas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeny.edu.common.EduException;
import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.stas.client.UcenterClient;
import com.zeny.edu.stas.entity.StatisticsDaily;
import com.zeny.edu.stas.entity.dto.SearchDto;
import com.zeny.edu.stas.mapper.StatisticsDailyMapper;
import com.zeny.edu.stas.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zeny
 * @since 2020-03-08
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * @param day
     * @return void
     * @Description 获取注册人数
     * @Date 2020/3/8 0008 16:51
     **/
    @Override
    public void getCountRegisterNum(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        ResponseResult result = ucenterClient.registerCount(day);
        Integer count = (Integer) result.getData().get("count");
        System.out.println("注册人数: " + count);

        Integer courseNum = RandomUtils.nextInt(100, 200);
        Integer videoViewNum = RandomUtils.nextInt(100, 200);
        Integer loginNum = RandomUtils.nextInt(100, 200);
        StatisticsDaily daily = new StatisticsDaily();
        daily.setCourseNum(courseNum);
        daily.setRegisterNum(count);
        daily.setVideoViewNum(videoViewNum);
        daily.setLoginNum(loginNum);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    /**
     * @param searchDto
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Description 根据时间范围查询返回数据
     * @Date 2020/3/9 0009 10:30
     **/
    @Override
    public Map<String, Object> getCountData(SearchDto searchDto) {
        if (StringUtils.isBlank(searchDto.getType())
                || StringUtils.isBlank(searchDto.getBegin())
                || StringUtils.isBlank(searchDto.getEnd())) {
            throw new EduException(20002, "搜索条件存在空值");
        }
        QueryWrapper<StatisticsDaily> wrapper = getWrapper(searchDto);
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);
        List<String> timeList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        dailyList.forEach(day -> {
            String dateCalculated = day.getDateCalculated();
            timeList.add(dateCalculated);
            switch (searchDto.getType()) {
                case "login_num":
                    countList.add(day.getLoginNum());
                    break;
                case "register_num":
                    countList.add(day.getRegisterNum());
                    break;
                case "video_view_num":
                    countList.add(day.getVideoViewNum());
                    break;
                case "course_num":
                    countList.add(day.getCourseNum());
                    break;
                default:
                    break;
            }
        });
        Map<String, Object> map = new HashMap<>();
        map.put("timeList",timeList);
        map.put("countList",countList);
        return map;
    }

    private QueryWrapper<StatisticsDaily> getWrapper(SearchDto searchDto) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", searchDto.getBegin(), searchDto.getEnd());
        String type = searchDto.getType();
        wrapper.select("date_calculated", type);
        return wrapper;
    }
}
